package com.bjesc_b2c.app.appraiset.mvp.view.activity;

import android.content.Intent;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.bjesc_b2c.app.appraiset.R;
import com.bjesc_b2c.app.appraiset.adapter.CarfromTwoAdapter;
import com.bjesc_b2c.app.appraiset.base.BaseActivity;
import com.bjesc_b2c.app.appraiset.mvp.presenter.GetCarForPresenter;
import com.bjesc_b2c.app.appraiset.mvp.presenter.impl.GetCarForPresenterImpl;
import com.bjesc_b2c.app.appraiset.mvp.view.CarForView;
import com.bjesc_b2c.app.appraiset.utils.LocalGroupSearch;
import com.liaoinstan.springview.container.DefaultHeader;
import com.liaoinstan.springview.widget.SpringView;

import java.util.ArrayList;
import java.util.List;

import app.pinggushi.bjesc.com.baserecyclerview.adapter.MultiItemTypeAdapter;
import butterknife.Bind;
import butterknife.OnClick;
import master_bean.CarForBean;

/**
 * Created by yidong on 2017/8/17.
 * 选择经销商 （车行）
 */

public class SelectDealerActivity extends BaseActivity implements CarForView,SpringView.OnFreshListener {

    @Bind(R.id.recycler_view)
    RecyclerView carfromTwoRecycler;
    @Bind(R.id.tv_title)
    TextView titleTv;
    @Bind(R.id.ed_text)
    EditText searchEd;
    @Bind(R.id.btn_other)
    Button otherBtn;
    @Bind(R.id.spring_view)
    SpringView mSpringView;

    private CarfromTwoAdapter twoAdapter;
    private List<CarForBean> carForBean = new ArrayList<>();
    private Intent intent;
    private LinearLayoutManager twoManager;
    private GetCarForPresenter carForPresenter;
    private String marketId;
    private int type = 1;
    private int refreshType = 1;
    private boolean isShowDialog = true;

    @Override
    protected int getLayout() {
        return R.layout.activity_select_dealer;
    }

    @Override
    protected void initEventAndData() {
        intent = new Intent();
        titleTv.setText("选择车行");
        searchEd.addTextChangedListener(textWatcher);
        mSpringView.setListener(this);
        mSpringView.setHeader(new DefaultHeader(this));
//        mSpringView.setFooter(new DefaultFooter(this));
        mSpringView.setType(SpringView.Type.FOLLOW);
    }

    @Override
    protected void createPresenter() {
        carForPresenter = new GetCarForPresenterImpl(SelectDealerActivity.this, this);
        carForPresenter.requestDealerforType(this, type,"");
    }

    private void initViews() {
        twoAdapter = null;
        carfromTwoRecycler.setAdapter(twoAdapter);
        twoManager = new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false);
        carfromTwoRecycler.setLayoutManager(twoManager);
        if (type == 1) {
            twoAdapter = new CarfromTwoAdapter(this, R.layout.layout_item_select_dealer, carForBean, CarfromTwoAdapter.TYPE_ONE);
        } else {
            twoAdapter = new CarfromTwoAdapter(this, R.layout.layout_item_carfrom_two, carForBean, CarfromTwoAdapter.TYPE_TWO);
        }
        carfromTwoRecycler.setAdapter(twoAdapter);
        twoAdapter.setOnItemClickListener(twoListener);
    }

    private MultiItemTypeAdapter.OnItemClickListener twoListener = new MultiItemTypeAdapter.OnItemClickListener() {
        @Override
        public void onItemClick(View view, RecyclerView.ViewHolder holder, int position) {
            isShowDialog = true;
            if (type == 1) {
                intent.putExtra("carForName", twoAdapter.getDatas().get(position).getName());
                intent.putExtra("carFofId", twoAdapter.getDatas().get(position).getDealer_id());//"29," +
                intent.setClass(SelectDealerActivity.this, PatrolShopActivity.class);
                startIntent(intent, 1);
            }else {
                marketId = twoAdapter.getDatas().get(position).getMarket_id();
                carForPresenter.requestDealerforType(mContext, refreshType,marketId);
                otherBtn.setText("上一步");
                type = 1;
            }
        }

        @Override
        public boolean onItemLongClick(View view, RecyclerView.ViewHolder holder, int position) {
            return false;
        }
    };
    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == 2) {
            isShowDialog = false;
            mSpringView.callFresh();
        }
    }
    @Override
    public void loadStart() {
        if (isShowDialog) {
            showLoading();
        }
    }

    @Override
    public void loadCompleted() {
        cancelLoading();
        if (mSpringView != null) {
            mSpringView.onFinishFreshAndLoad();
        }
    }

    @Override
    public void loadFailure() {
        cancelLoading();
        if (mSpringView != null) {
            mSpringView.onFinishFreshAndLoad();
        }
    }

    @Override
    public void onRefresh() {
        isShowDialog = false;
        if (refreshType == 1) {
            carForPresenter.requestDealerforType(this, refreshType,"");
        }else {
            carForPresenter.requestDealerforType(mContext, refreshType,marketId);
        }
    }

    @Override
    public void onLoadmore() {

    }

    @OnClick({R.id.iv_return, R.id.btn_other})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.iv_return:
                finish();
                break;
            case R.id.btn_other:
                isShowDialog = true;
                if (type == 1) {
                    type = 2;
                    refreshType = 2;
                    otherBtn.setText("我的车行");
                } else {
                    type = 1;
                    refreshType = 1;
                    otherBtn.setText("其他车行");
                }
                carForPresenter.requestDealerforType(this, refreshType,"");
                break;
        }

    }


    @Override
    public void getCarForDealer(List<CarForBean> listbean) {
        if (listbean == null) {
            if (twoAdapter != null) {
                carfromTwoRecycler.setAdapter(null);
            }
            return;
        }
//        Collections.sort(listbean, new Comparator<CarForBean>() {
//            public int compare(CarForBean carBrandBean1, CarForBean carBrandBean2) {
//                if (carBrandBean1.getFirstLetter().charAt(0) > carBrandBean2.getFirstLetter().charAt(0)) {
//                    return 1;
//                }
//                if (carBrandBean1.getFirstLetter().charAt(0) == carBrandBean2.getFirstLetter().charAt(0)) {
//                    return 0;
//                }
//                return -1;
//            }
//        });
        carForBean = listbean;
        initViews();
    }

    TextWatcher textWatcher = new TextWatcher() {
        @Override
        public void onTextChanged(CharSequence s, int arg1, int arg2, int arg3) {
            if (s != null && s.length() > 0) {
                List<CarForBean> listG = LocalGroupSearch.searchGroup(s, (ArrayList<CarForBean>) carForBean);
                if (listG != null && listG.size() > 0) {
                    if (twoAdapter != null)
                        twoAdapter.refresh(listG);
                } else {
                    showToast("搜索为空");
                }

            } else {
                if (twoAdapter != null)
                    twoAdapter.refresh(carForBean);
            }
        }

        @Override
        public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,
                                      int arg3) {
        }

        @Override
        public void afterTextChanged(Editable arg0) {
        }
    };
}
